package com.wholeintech.dao;

import java.util.List;

import com.wholeintech.entity.BoxInfo;
import com.wholeintech.utils.DatabaseUtils;


import android.content.Context;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

/**
 * 场景数据操作类
 *
 * @author Administrator
 */
public class BoxInfoDao {
    private final static String TAG = "BoxInfoDao";
    private Context mContext = null;

    public BoxInfoDao(Context context) {
        this.mContext = context;
    }

    /**
     * 新增
     *
     * @param boxInfo
     * @return
     */
    public boolean add(BoxInfo boxInfo) {
        boolean bRet = false;
        DbManager db = DatabaseUtils.DeviceDbUtils();
        try {
            db.save(boxInfo);
            bRet = true;
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return bRet;
    }

    /**
     * 删除信息
     *
     * @param intId
     */
    public void delete(int intId) {
        DbManager db = DatabaseUtils.DeviceDbUtils();

        try {
            db.deleteById(BoxInfo.class, intId);
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 删除信息
     */
    public void deleteAll() {
        DbManager db = DatabaseUtils.DeviceDbUtils();

        try {
            db.dropTable(BoxInfo.class);
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 获取所有盒子信息
     *
     * @return
     */
    public List<BoxInfo> getBoxInfoList() {
        DbManager db = DatabaseUtils.DeviceDbUtils();
        List<BoxInfo> list = null;

        try {
            list = db.findAll(BoxInfo.class);
            if (list != null) {
                list.size();
            }

        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return list;
    }

    /**
     * 获取指定盒子信息
     *
     * @param strBoxId
     * @return
     */
    public BoxInfo getBoxInfoByBoxId(String strBoxId) {
        DbManager db = DatabaseUtils.DeviceDbUtils();
        BoxInfo info = null;

        try {
            info = db.selector(BoxInfo.class).where("boxId", "=", strBoxId).findFirst();
        } catch (DbException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                db = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return info;
    }
}
